<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <base href="<%=basePath%>">

    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <link rel="stylesheet" href="css/denlu.css">
    <script type="text/javascript" src=" <%=request.getContextPath()%>/js/jquery.js"></script>
    <script>

        $(function () {
            $.ajax({
                url: "<%=request.getContextPath()%>/queryNoticServlet",
                type: "post",
                dataType: "json",
                success: function (data) {

                    debugger
                    for (var i = 0; i <= data.length; i++) {
                        $("#noticServlet").append("<tr><td>" + (i + 1) + "</td><td>" + data[i].noticeText + "</td><td>" + data[i].noticeMan + "</td><td>" + data[i].noticType + "</td><td>" + data[i].createTime + "</td><td><a onclick=' dell(this)' name='" + data[i].id + "'> 删除</a><td><a onclick=' upd(this)' name='" + data[i].id + "'> 修改</a></td></tr>")
                    }
                }
            })
        })

        function dell(a) {
            del(a.name)
        }

        function upd(id) {
            debugger
            $.ajax({
                //几个参数需要注意一下
                type: "POST",//方法类型
                dataType: "json",//预期服务器返回的数据类型
                url: "<%=request.getContextPath()%>/queryNoticServlet",//url
                data: {"id": id.name},
                success: function (result) {
                    debugger

                    $("#form1").prepend("ID: <input type='text' readonly='true' name='id' value='" + result.id + "'><br/>", this)
                    $("[name='noticeText']").attr("value", result.noticeText);

                    $("#type").val(result.noticType)
                    $("#tj").val("修改")
                    //$("input[name='noticeText']").value = result.noticeText;
                },
                error: function () {
                    alert("异常！");
                }
            });

        }

        function del(id) {
            $.ajax({
                //几个参数需要注意一下
                type: "POST",//方法类型
                dataType: "text",//预期服务器返回的数据类型
                url: "<%=request.getContextPath()%>/deleteNotic",//url
                data: {"id": id},
                success: function (result) {

                    alert(result)
                    location.reload()
                },
                error: function () {
                    alert("异常！");
                }
            });

        }

        function sumit() {
            debugger
            $.ajax({
                //几个参数需要注意一下
                type: "POST",//方法类型
                dataType: "text",//预期服务器返回的数据类型
                url: "<%=request.getContextPath()%>/insertNoticServlet",//url
                data: $('#form1').serialize(),
                success: function (result) {

                    alert(result);

                    location.reload()
                },
                error: function () {
                    alert("异常！");
                }
            });
        }


    </script>
    <style>
        table, table tr th, table tr td {
            border: 10px solid #0094ff;
        }

        table {
            width: 1800px;
            min-height: 25px;
            line-height: 25px;
            text-align: center;
            border-collapse: collapse;
        }
    </style>
</head>

<body>

<jsp:include page="page.jsp"/>

<table style="border:1px solid #0094ff">
    <thead>

    <tr>
        <th>编号</th>
        <th>公告信息</th>
        <th>创建人/编辑人</th>
        <th>公告类型</th>
        <th>创建时间</th>
        <th colspan="2">操作</th>
    </tr>
    </thead>
    <tbody id="noticServlet">

    </tbody>
</table>
<hr/>
<form id="form1" onsubmit="return false" action="submit()" method="post">
    公 告 信 息:<input type="text" name="noticeText"></br>
    公 告 类 型:
    <select name="type" id="type">
        <option value="-1">请选择</option>
        <option value="1">学校公告</option>
        <option value="0">班级公告</option>
    </select></br>
    <input type="button" value="发布" onclick="sumit()" id="tj"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="reset" value="重置"></br>
</form>

</body>


<html/>